﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Namespace Arwani
    Partial Class Backend_ManageGallery
        Inherits System.Web.UI.Page

        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim arwCon As New ArwaniController

            If Me.flProductImage.PostedFile.FileName <> "" Then
                Dim strQ As String
                Dim ext As String = IO.Path.GetExtension(Me.flProductImage.PostedFile.FileName)
                Dim strFileName As String = Guid.NewGuid.ToString() & ext
                Me.flProductImage.PostedFile.SaveAs(Server.MapPath("../Products/" & strFileName))
                Call ResizeImages(strFileName, "Thumb_Single_" & strFileName, 953, 225)
                Call ResizeImages(strFileName, "Thumb_Double_" & strFileName, 440, 230)
                Call ResizeImages(strFileName, "Thumb_Four_" & strFileName, 195, 200)
                Call ResizeImages(strFileName, "Thumb_Six_" & strFileName, 133, 175)
                strQ = "insert into product_images (productimage,productid) values ('" & strFileName & "'," & CInt(Request.QueryString("pid")) & ")"

                arwCon.ExecuteNonQuery(strQ)
            End If
            Call loadData()
        End Sub
        Public Function getImage(ByVal imgSrc As String) As String
            Dim strReturn As String = "images/main_banner.png"
            If File.Exists(Server.MapPath("../Products/" & imgSrc)) Then
                strReturn = "../Products/" & imgSrc
            End If
            Return strReturn
        End Function
        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageProducts.aspx")
        End Sub
        Sub ResizeImages(ByVal FileName As String, ByVal NewFileName As String, ByVal intWidth As Integer, ByVal intHeight As Integer)


            '        intWidth = 178 '*** Fix Width ***'  
            '        intHeight = 178   '*** If = 0 Auto Re-Cal Size ***'  

            Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("../Products/" & FileName))

            Dim objBitmap As Bitmap
            '*** Calculate Height ***'  
            If intHeight > 0 Then
                objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
            Else
                If objGraphic.Width > intWidth Then
                    Dim ratio As Double = objGraphic.Height / objGraphic.Width
                    intHeight = ratio * intWidth
                    objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
                Else
                    objBitmap = New Bitmap(objGraphic)
                End If
            End If

            '*** Save As  ***'  
            objBitmap.Save(Server.MapPath("../Products/" & NewFileName), objGraphic.RawFormat)

            '*** Close ***'  
            objGraphic.Dispose()

            '*** Nothing ***'  
            objBitmap = Nothing
            objGraphic = Nothing

        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then
                If Page.IsPostBack Then Exit Sub
                Call loadData()
            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadData()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from product_images where productid=" & CInt(Request.QueryString("pid"))
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.dtProductImages.DataSource = dtTable
            Me.dtProductImages.DataBind()

        End Sub

        Protected Sub dtProductImages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtProductImages.ItemCommand
            If e.CommandName = "Delete" Then
                Dim arwCon As New ArwaniController
                Dim strQ As String = ""
                strQ = "delete from product_images where productimage='" & (e.CommandArgument).ToString() & "'"
                arwCon.ExecuteNonQuery(strQ)
                Try
                    If File.Exists(Server.MapPath("../Products/" & (e.CommandArgument).ToString())) Then
                        File.Delete(Server.MapPath("../Products/" & (e.CommandArgument).ToString()))
                        File.Delete(Server.MapPath("../Products/Thumb_Single_" & (e.CommandArgument).ToString()))
                        File.Delete(Server.MapPath("../Products/Thumb_Double_" & (e.CommandArgument).ToString()))
                        File.Delete(Server.MapPath("../Products/Thumb_Four_" & (e.CommandArgument).ToString()))
                        File.Delete(Server.MapPath("../Products/Thumb_Six_" & (e.CommandArgument).ToString()))
                    End If
                Catch ex As Exception

                End Try
                Call loadData()
            End If
        End Sub

        Protected Sub dtProductImages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dtProductImages.ItemDataBound
            Try
                Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
                lnkDelete.Attributes.Add("onclick", "return confirm('Confirm delete this Image?')")
            Catch ex As Exception
            End Try
        End Sub
    End Class
End Namespace